package com.cresch.ical.downloader.ical;
import java.text.ParseException;


/**
 * 
 * @author Resch Christian
 *
 */
public class EventMapper {
	
	public static VEvent stringToEvent(String eventString) {
		VEvent vEvent = null;
		Splitter stringSplitter = new Splitter();
		ElementSplitter splitter = new ElementSplitter(stringSplitter);
		splitter.setInput(eventString);
		
		while (true) {
			
			// get UID
			String uid = splitter.singleStringElement(VEvent.UID, VCalendar.LINESEPERATOR);
			vEvent = new VEvent(uid);	
			
			// get event start
			try {
				vEvent.setStart(splitter.singleDateElement(VEvent.DTSTART, VCalendar.LINESEPERATOR, VEvent.DATETIMEFORMAT));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				break;
			}
			
			// get event end
			try {
				vEvent.setEnd(splitter.singleDateElement(VEvent.DTEND, VCalendar.LINESEPERATOR, VEvent.DATETIMEFORMAT));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			// get event location
			vEvent.setLocation(splitter.singleStringElement(VEvent.LOCATION, VCalendar.LINESEPERATOR));
			
			// get event summary
			vEvent.setSummary(splitter.singleStringElement(VEvent.SUMMARY, VCalendar.LINESEPERATOR));			
			
			// get event description
			vEvent.setDescription(splitter.singleStringElement(VEvent.DESCRIPTION, VCalendar.LINESEPERATOR));			
			
			break;
		}
		
		return vEvent;
	}
}
